# syntax=docker/dockerfile:1
# 第一阶段：构建前端应用
FROM node:20-slim AS build
WORKDIR /app

# 复制依赖配置文件
COPY package.json package-lock.json* pnpm-lock.yaml* ./

# 可配置的国内镜像（默认使用 npmmirror）
ARG NPM_REGISTRY=https://registry.npmmirror.com
RUN npm config set registry ${NPM_REGISTRY} && \
    corepack enable && corepack prepare pnpm@9.12.3 --activate || true

# 安装依赖
RUN pnpm install --frozen-lockfile || npm ci

# 复制源代码
COPY . .

# 接收构建时环境变量
ARG VITE_API_BASE_URL=http://localhost:5001/api
ARG VITE_FEATURE_AUTH=true
ARG VITE_FEATURE_CRUD=true
ARG VITE_FEATURE_EXCEL=true
ARG VITE_FEATURE_GH_TRENDING=true
ARG VITE_FEATURE_AI=true
ARG VITE_FEATURE_CRAWLER=false
ARG VITE_FEATURE_RBAC=false

# 设置环境变量供 Vite 构建使用
ENV VITE_API_BASE_URL=${VITE_API_BASE_URL}
ENV VITE_FEATURE_AUTH=${VITE_FEATURE_AUTH}
ENV VITE_FEATURE_CRUD=${VITE_FEATURE_CRUD}
ENV VITE_FEATURE_EXCEL=${VITE_FEATURE_EXCEL}
ENV VITE_FEATURE_GH_TRENDING=${VITE_FEATURE_GH_TRENDING}
ENV VITE_FEATURE_AI=${VITE_FEATURE_AI}
ENV VITE_FEATURE_CRAWLER=${VITE_FEATURE_CRAWLER}
ENV VITE_FEATURE_RBAC=${VITE_FEATURE_RBAC}

# 构建应用
RUN pnpm build || npm run build

# 第二阶段：使用 Nginx 提供服务
FROM nginx:alpine AS runtime

# 复制构建产物
COPY --from=build /app/dist /usr/share/nginx/html

# 复制 Nginx 配置文件
COPY nginx.conf /etc/nginx/conf.d/default.conf

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget --quiet --tries=1 --spider http://localhost/health || exit 1

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]


